package com.shujia

import scala.io.{BufferedSource, Source}

object Demo22Implicit3 {
  def main(args: Array[String]): Unit = {
    // 3、隐式转换类
    val path: String = "Scala/data/words.txt"
    val rf1: ReadFile = new ReadFile(path)
    rf1.read_file()
    println(rf1.lines)

    val lines: List[String] = "Scala/data/students.txt".read_file()
    "Scala/data/students.txt".printR()

  }

  /**
   * 隐式转换类
   * 动态地给 与ReadFile类所需要地构造方法的参数path 同类型的变量
   * 添加ReadFile类所有的方法
   * 默认的构造方法只能接收一个参数
   */
  implicit class ReadFile(path: String) {
    var lines: List[String] = _

    def read_file(): List[String] = {
      val br: BufferedSource = Source.fromFile(path)
      lines = br.getLines().toList
      br.close()
      lines
    }

    def printR(): Unit = {
      println("ReadFile中的printR方法")
    }
  }
}


